package com.tanhua.dubbo.api;

import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.mapper.AnalysisByDayMapper;
import com.tanhua.dubbo.mapper.LogMapper;
import com.tanhua.dubbo.utils.ComputeUtil;
import org.apache.dubbo.config.annotation.Service;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

@Service
public class AnalysisByDayApiImpl implements AnalysisByDayApi {


    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public void analysisByDay(String todayStr) {
        AnalysisByDay analysisByDay = new AnalysisByDay();

        analysisByDay.setRecordDate(new DateTime(todayStr).toDate());
//        查询当天登录的人数
        Integer numLogin  = logMapper.queryNumRegisteredOrLogin(todayStr,"0101");
        analysisByDay.setNumLogin(numLogin);
//        查询当天注册的人数
        Integer numRegistered =logMapper.queryNumRegisteredOrLogin(todayStr,"0102");
        analysisByDay.setNumRegistered(numRegistered);
//        查询当天有多少人活跃
        Integer numActive = logMapper.queryNumActive(todayStr);
        analysisByDay.setNumActive(numActive);
//        当天有多少留存人数
        String yestodyStr = new DateTime(todayStr).minusDays(1).toString("yyyy-MM-dd");
        Integer  numRetention1d = logMapper.queryNumRetention1d(todayStr,yestodyStr);
        analysisByDay.setNumRetention1d(numRetention1d);

        analysisByDayMapper.insert(analysisByDay);
    }

    @Override
    public AnalysisSummaryVo summary(String todayStr) {
        AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();
//        select  sum(num_registered) as num from tb_analysis_by_day
        Long cumulativeUsers = analysisByDayMapper.queryCumulativeUsers();
        summaryVo.setCumulativeUsers(cumulativeUsers);// 累计用户

        String passMonthStr = new DateTime(todayStr).minusDays(30).toString("yyyy-MM-dd");
//        select  sum(num_active)  as num from tb_analysis_by_day  where record_date>='2021-02-13' and  record_date<='2021-03-13'

        Long activePassMonth = analysisByDayMapper.queryActivePass(todayStr,passMonthStr);
        summaryVo.setActivePassMonth(activePassMonth); //过去30天活跃用户


//        select  sum(num_active)  as num from tb_analysis_by_day  where record_date>='2021-03-06' and  record_date<='2021-03-13'
        String passWeekStr = new DateTime(todayStr).minusDays(7).toString("yyyy-MM-dd");
        Long activePassWeek = analysisByDayMapper.queryActivePass(todayStr,passWeekStr);
        summaryVo.setActivePassWeek(activePassWeek); // 过去7天活跃用户

//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-13' and type='0102'
        Integer newUsersToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0102");
        summaryVo.setNewUsersToday(newUsersToday.longValue()); //今日新增用户数

//        newUsersYesToDay	昨日新增用户
//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-12' and type='0102'
        String yesToDayStr = new DateTime(todayStr).minusDays(1).toString("yyyy-MM-dd");
        Integer newUsersYesToday = logMapper.queryNumRegisteredOrLogin(yesToDayStr, "0102");
//        newUsersTodayRate 今日新增用户涨跌率	   今天-昨天 * 100 /昨天
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday, newUsersYesToday);
        summaryVo.setNewUsersTodayRate(newUsersTodayRate); //今日新增用户涨跌率


//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-13' and type='0101'
        Integer loginTimesToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0101");
        summaryVo.setLoginTimesToday(loginTimesToday.longValue()); //今日登录次数

//        loginTimesYesToDay	昨日登录次数
//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-12' and type='0101'
//        loginTimesTodayRate	今日登录次数涨跌率	今天-昨天 * 100 /昨天
        Integer loginTimesYesToday = logMapper.queryNumRegisteredOrLogin(yesToDayStr, "0101");
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, loginTimesYesToday);
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate); //今日登录次数涨跌率

//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-13'
        Integer activeUsersToday = logMapper.queryNumActive(todayStr);
        summaryVo.setActiveUsersToday(activeUsersToday.longValue());//今日活跃用户
//        activeUsersYesToDay	昨天活跃用户
//        select count( DISTINCT user_id) from tb_log where log_time='2021-03-12'
        Integer activeUsersYesToday = logMapper.queryNumActive(yesToDayStr);

        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, activeUsersYesToday);
        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);  //今日活跃用户涨跌率

        return summaryVo;
    }
}
